import AsyncStorage from '@react-native-community/async-storage';
// import {api, http} from '../../http';
import api from '../../http/api';

const fetchFn = async e => {
  const a = await new Promise(res => {
    setTimeout(() => {
      res(e);
    }, 4000);
  });
  return a;
};
const reSetUser = async userInfo => {
  // await AsyncStorage.removeItem('userInfo');
  await AsyncStorage.setItem('userInfo', JSON.stringify(userInfo));
  return userInfo;
};
const getButtonData = async param => {
  console.log(param, 'param');
  // const res = await http.get(api.LOGIN_API, param);
  let query = '';
  const headers = new Headers();
  Object.entries(param).forEach(([key, value], index) => {
    query += `${index === 0 ? '?' : '&'}${key}=${value}`;
  });
  const response = await fetch(api.baseURL + api.LOGIN_API + query, {
    method: 'GET',
    headers,
  });
  console.log(response, 'response');
  const res = await response.json();
  return res;
};
export default {
  namespace: 'user',
  state: {
    name: '345',
    userInfo: '',
    isLogin: false,
    isShowLoginRegister: false,
  },
  // action 返回一个Promise
  effects: {
    *setPro({payload: todo}, {put, call, select}) {
      // data.name = data.data;
      // 获取state
      const da = yield select(state => {
        console.log(state);
        return state;
      });
      const res = yield call(fetchFn, todo); // 异步函数
      // const result = yield call(fetchFn, '/todos');
      // console.log(data.data, 'setPro');
      console.log('111111111111111', res, da);
      yield put({type: 'setVal', payload: res});
      return 'todo'; // 返回Promise 数据
    },
    *setUserInfo({payload: userInfo}, {put, call, select}) {
      console.log(userInfo, 'setUserInfo');
      if (!userInfo) {
        userInfo = '';
      }
      const res = yield call(reSetUser, userInfo);
      yield put({type: 'setUserState', payload: res});
      return userInfo;
    },
    *getButtonData({payload: param}, {put, call, select}) {
      const res = yield call(getButtonData, param);
      return res;
    },
  },
  reducers: {
    logOut(data) {
      console.log(123123);
      data.name = '';
      data.userInfo = '';
      data.isLogin = false;
      return data;
    },
    setVal(data, res) {
      data.name = res.payload;
      // console.log(data, 'aaa');
      return data; // 必须返回state
    },
    setIsLogin(data, res) {
      console.log('login', res.payload);
      data.isLogin = res.payload;
      data.name = '***@patpat.com';
      data.userInfo = '';
      data.isLogin = true;
      data.isShowLoginRegister = false;
      return data; // 必须返回state
    },
    setShowLoginRegister(data, res) {
      console.log('ShowLoginRegister', res.payload);
      data.isShowLoginRegister = res.payload;
      return data; // 必须返回state
    },
    setUserState(data, res) {
      // console.log(data, 'dddd');
      data.userInfo = res.payload;
      return data; // 必须返回state
    },
  },
  // 初始化数据，订阅一个数据源， 并派发 {history} = undefined
  subscriptions: {
    async pm({dispatch}) {
      console.log('res34234234');
      const a = await new Promise((resole, reject) => {
        setTimeout(() => {
          console.log('data...............');
          resole('***@patpat.com');
        }, 2000);
      });
      console.log(a);
      dispatch({
        type: 'setVal',
        payload: a,
      });
    },
    async initUserInfo({dispatch}) {
      console.log('initUserInfo');
      const a = await new AsyncStorage.getItem('userInfo');
      console.log(a, 'initUserInfo3');
      dispatch({
        type: 'setUserInfo',
        payload: 'token',
      });
    },
  },
};
